package com.ly.hotel.view.common;

import java.lang.ref.WeakReference;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;

public class TimingMsgDialog extends AlertDialog {
	private Context context;

	LinearLayout mWebViewContainer;
	protected MyApplication app;
	private String msg = null;
	private boolean autoClose = false;
	private CountTimer countTimer;  
	private TextView msgView;
	private ThisHandler handlerMsg = new ThisHandler(this);
	
	
	public TimingMsgDialog(Context context, int theme) {
	    super(context, theme);
	    this.context = context;
	}

	public TimingMsgDialog(Context context) {
	    super(context);
	    this.context = context;
	}

	public TimingMsgDialog(Context context, boolean autoClose) {
	    super(context);
	    this.context = context;
	    this.autoClose = autoClose;
	}
	  
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.timing_msg_dialog);
//		this.setFinishOnTouchOutside(false);
		initView();
		if(autoClose)
			countTimer = new CountTimer(10000,1000);  
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		if(autoClose)
			countTimer.start();  
	}

	private void initView(){
//		closeBtn = (TextView) findViewById(R.id.closeBtn);
		if(msg != null){
			msgView = (TextView) findViewById(R.id.msg_tips);
			msgView.setText(msg);
			msgView.setTextSize(30);
			msgView.setTextColor(Color.WHITE);
		}
		
		((TextView) findViewById(R.id.closeBtn)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(autoClose && countTimer != null)
	        		countTimer.cancel(); 
				dismiss();
			}
		});
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * handler处理消息机制
	 */
	private static class ThisHandler extends Handler {
		private WeakReference<TimingMsgDialog> wrActivity;

		public ThisHandler(TimingMsgDialog activity) {
			wrActivity = new WeakReference<TimingMsgDialog>(activity);
		}

		@Override
		public void handleMessage(Message message) {
			TimingMsgDialog act = wrActivity.get();
			if (act != null) {
				switch (message.what) {
				case 0:
					break;	

				}
			}
		}
	}
	
	 class CountTimer extends CountDownTimer   
	    {  
	  
	        /** 
	         * 构造函数 
	         * @param millisInFuture 倒计的时间数,以毫秒为单位 
	         * @param countDownInterval 倒计每秒中间的间隔时间,以毫秒为单位 
	         */  
	        public CountTimer(long millisInFuture, long countDownInterval)  
	        {  
	            super(millisInFuture, countDownInterval);  
	        }  
	  
	        @Override  
	        public void onFinish()  
	        {  
	        	if(autoClose && countTimer != null)
	        		countTimer.cancel(); 
	        	
	            dismiss(); 
	        }  
	  
	        @Override  
	        public void onTick(long millisUntilFinished)  
	        {  
//	            tv_tips.setText(_activity.getString(R.string.issaving)+millisUntilFinished / 1000 + _activity.getString(R.string.second));  
	        }  
	          
	    }  
	      
}
